package org.lendoo.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.entity.Address;
import org.lendoo.web.enumeration.ServiceResultEnum;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.pojo.vo.RelUserAddressVo;
import org.springframework.stereotype.Service;
import org.lendoo.web.mapper.RelUserAddressMapper;
import org.lendoo.web.entity.RelUserAddress;
import org.lendoo.web.service.RelUserAddressService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * RelUserAddressService接口的实现类
 */
@Slf4j
@Service
public class RelUserAddressServiceImpl extends ServiceImpl<RelUserAddressMapper, RelUserAddress> implements RelUserAddressService {

    @Resource
    private ServiceHelper serviceHelper;

    /**
     * 新增
     * @param relUserAddressVo 用户和地址的关系表
     * @return 添加成功返回1，添加失败返回-1
     */
    @Override
    public ServiceResult<Boolean> add(RelUserAddressVo relUserAddressVo) {
        Address address = new Address();
        address.setDetail(relUserAddressVo.getDetail());
        address.setProvince(relUserAddressVo.getProvince());
        address.setCity(relUserAddressVo.getCity());
        address.setRegion(relUserAddressVo.getRegion());
        address.setRealName(relUserAddressVo.getRealName());
        address.setMobile(relUserAddressVo.getMobile());
        address.setCreateTime(new Date());
        address.setUpdateTime(new Date());
        serviceHelper.getAddressMapper().insert(address);

        RelUserAddress relUserAddress = new RelUserAddress();
        relUserAddress.setAddressId(address.getId());
        relUserAddress.setUserId(relUserAddressVo.getUserId());
        relUserAddress.setIsDefault(relUserAddressVo.getIsDefault());
        baseMapper.insert(relUserAddress);

        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 批量导入
     * @param relUserAddressList 用户和地址的关系表（列表）
     * @return 批量导入成功返回1，批量导入失败返回-1
     */
    @Override
    public ServiceResult<Boolean> batchAdd(List<RelUserAddress> relUserAddressList) {
        baseMapper.insertBatchSomeColumn(relUserAddressList);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 根据id更新
     * @param relUserAddress 用户和地址的关系表
     * @return 更新成功返回1，更新失败返回-1
     */
    @Override
    public ServiceResult<Boolean> updateForId(RelUserAddress relUserAddress) {
        baseMapper.updateById(relUserAddress);
        return new ServiceResult<>(ServiceResultEnum.UPDATE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.UPDATE_SUCCESS.getMessage());
    }

    /**
     * 根据id删除
     * @param id 主键
     * @return 删除成功返回1，删除失败返回-1
     */
    @Override
    public ServiceResult<Boolean> deleteById(Serializable id) {
        baseMapper.deleteById(id);
        return new ServiceResult<>(ServiceResultEnum.DELETE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.DELETE_SUCCESS.getMessage());
    }

    /**
     * 根据id查询
     * @param id 主键
     * @return 返回查询结果
     */
    @Override
    public ServiceResult<RelUserAddress> selectById(Serializable id) {
        RelUserAddress relUserAddress = baseMapper.selectById(id);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), relUserAddress, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据条件，分页查询，升序/降序排列
     * @param relUserAddress 用户和地址的关系表
     * @param pageNo 页号
     * @param pageSize 每页行数
     * @return 返回查询结果列表
     */
    @Override
    public ServiceResult<PageInfo<RelUserAddress>> page(RelUserAddress relUserAddress, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        LambdaQueryWrapper<RelUserAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<RelUserAddress> relUserAddressList = baseMapper.selectList(lambdaQueryWrapper);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), new PageInfo<>(relUserAddressList), Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 查询所有记录
     * @return
     */
    @Override
    public ServiceResult<List<RelUserAddress>> findAll() {
        List<RelUserAddress> relUserAddressList = baseMapper.selectList(null);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), relUserAddressList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据usrId，查询用户的地址数量
     * @param userId
     * @return
     */
    @Override
    public ServiceResult<Integer> countByUserId(Integer userId) {
        LambdaQueryWrapper<RelUserAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RelUserAddress::getUserId, userId);
        Integer count = baseMapper.selectCount(lambdaQueryWrapper);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), count, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据usrId，查询用户的地址列表
     * @param userId
     * @return
     */
    @Override
    public ServiceResult<List<org.lendoo.web.pojo.dto.RelUserAddressDto>> findByUserId(Integer userId) {
        List<org.lendoo.web.pojo.dto.RelUserAddressDto> relUserAddressDtoList = serviceHelper.getRelUserAddressMapper().findByUserId(userId);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), relUserAddressDtoList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }
}
